import { defineStore } from "pinia";
import router from "../router";
export const userStore = defineStore("user", {
    state: () => ({
        score: 0,
        username: "",
        shop: {},
        shopList: {},
        defaultAddress: null,
        address: null,
        isShow: false,
        order: undefined,
        loginauth: null,
        goodDetail: null,
        isExchange: false,
        glSortType: "",
        glSortUD: 0,
        elSortType: 0,

        userInfo: {
            shopName: "",
            connectName: "",
            connectPhone: "",
            connectAddress: "",
            province: "",
            city: "",
            district: "",
            schemeType: "",
        },
        activityData: {},
        myActivityData: {},
    }),
    getters: {
        shopData: (state) => {
            let num = 0,
                score = 0;
            for (let i in state.shop) {
                const tmp = state.shop[i];
                num += tmp.num;
                score += tmp.num * tmp.needPoints;
            }
            return {
                num: num,
                score: score,
            };
        },
        shopListData: (state) => {
            let num = 0,
                score = 0;
            for (let i in state.shopList) {
                const tmp = state.shopList[i];
                num += tmp.num;
                score += tmp.num * tmp.needPoints;
            }
            return {
                num: num,
                score: score,
            };
        },
    },
    actions: {
        setMyActivityData(data) {
            this.myActivityData = data;
        },
        setActivityData(data) {
            this.activityData = data;
        },
        setUserInfo(data) {
            this.userInfo = data;
        },

        setElSortType(i) {
            this.elSortType = i;
        },
        setGlSortUD(i) {
            this.glSortUD = i;
        },
        setGlSortType(i) {
            this.glSortType = i;
        },
        setOrder(data) {
            this.order = data;
        },
        editAddress(id) {
        },
        resetAddress(data) {
            this.address = data;
            const tmpd = data.filter((v) => {
                return v.isDefault === 1;
            });
            if (tmpd.length > 0) {
                if (!this.defaultAddress) {
                    this.defaultAddress = tmpd[0];
                }
            } else {
                this.defaultAddress = null;
            }
        },
        showAddress() {
            this.isShow = !this.isShow;
        },
        addAddress() {
            router.push("/myinfo");
        },
        addShop(data, cb) {
            const { id } = data;
            if (this.shop[id]) {
                if (this.shop[id].num >= this.shop[id].exchangeNum) {
                    if (typeof cb !== "undefined") {
                        cb();
                    }
                } else {
                    this.shop[id].num++;
                }
            } else {
                data.num = 1;
                this.shop[id] = JSON.parse(JSON.stringify(data));
            }
        },
        reduceShop(id) {
            // if (this.shop[id].num === 1) {
            delete this.shop[id];
            // }
            // else {
            //     this.shop[id].num--;
            // }
        },
        confirmShop(data) {
            if (data) {
                this.shopList = {};
                this.shopList[data.id] = JSON.parse(JSON.stringify(data));
                this.shopList[data.id]["num"] = 1;
            } else {
                if (Object.keys(this.shop).length > 0) {
                    this.shopList = JSON.parse(JSON.stringify(this.shop));
                } else {
                    alert("购物车为空");
                    return false;
                }
            }
            if (this.score < this.shopData.score) {
                alert("积分不足");
                return false;
            }
            router.push("/orderdetail");
        },
        getStatus(status) {
            if (status === 0) {
                return {
                    cls: "",
                    text: "未发货",
                };
            } else if (status === 1) {
                return {
                    cls: "get",
                    text: "已发货",
                };
            } else {
                return {
                    cls: "finish",
                    text: "已完成",
                };
            }
        },
    },
    persist: {
        enabled: true,
    },
});
